﻿Imports DTO
Imports System.Data.SqlClient
Public Class PhieuGiaoDAO


    Dim dp As New DataProvider
    Function LoadPG() As DataTable
        Dim phieugiao As New DataTable
        phieugiao = dp.ExecuteQuery("LoadNhanHieu", Nothing)
        Return phieugiao
    End Function



    Public Sub ThemPG(ByVal phieugiao As PhieuGiaoDTO)
        Dim dt As New DataProvider
        Dim para As SqlParameter() = New SqlParameter(4) {}
        para(0) = New SqlParameter("@MaPG", phieugiao.MaPG)
        para(1) = New SqlParameter("@Ngayxuat", phieugiao.NgayGiao)
        para(2) = New SqlParameter("@MaSP", phieugiao.MaHang)
        para(3) = New SqlParameter("@SoLuong", phieugiao.SoLuong)
        para(4) = New SqlParameter("@MaKho", phieugiao.MaKho)
        dt.ExecuteNonQuery("InsertPG", para)

    End Sub


    Function LayTheoID(ByVal PG As Integer) As PhieuGiaoDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaPG", PG)
        Dim dt = cn.ExecuteQuery("LayPGTheoID", para)
        Dim phieugiao As New PhieuGiaoDTO()
        If dt.Rows.Count <> 0 Then
            phieugiao.MaPG = Integer.Parse(dt.Rows(0).Item("MaPG").ToString())
            phieugiao.NgayGiao = DateTime.Parse(dt.Rows(0).Item("Ngayxuat").ToString())
            phieugiao.MaKho = Integer.Parse(dt.Rows(0).Item("MaKho").ToString())
            phieugiao.MaHang = Integer.Parse(dt.Rows(0).Item("MaSP").ToString())
            phieugiao.SoLuong = Integer.Parse(dt.Rows(0).Item("Soluong").ToString())
        End If
        Return phieugiao
    End Function

    Function LayTatCa() As Collections.IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("layTatCaPG")
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim phieugiao As New PhieuGiaoDTO()
                phieugiao.MaPG = Integer.Parse(dr.Item("MaPG").ToString())
                phieugiao.MaHang = Integer.Parse(dr.Item("MaSP").ToString())
                phieugiao.MaKho = Integer.Parse(dr.Item("MaKho").ToString())
                phieugiao.NgayGiao = DateTime.Parse(dr.Item("Ngayxuat").ToString())
                phieugiao.SoLuong = Integer.Parse(dr.Item("SoLuong").ToString())
                array.Add(phieugiao)
            Next
        End If
        Return array
    End Function

    Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LoadPG")
    End Function

    Sub Them(ByVal PG As PhieuGiaoDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@MaSP", PG.MaHang)
        para(1) = New SqlParameter("@ngayxuat", PG.NgayGiao)
        para(2) = New SqlParameter("@makho", PG.MaKho)
        para(3) = New SqlParameter("@soluong", PG.SoLuong)
        cn.ExecuteNonQuery("InsertPG", para)
    End Sub



    Public Sub Xoa(ByVal id As String)
        Dim cn As New DataProvider()
        Dim parar = New SqlParameter("@Mapg", id)
        cn.ExecuteNonQuery("DeletePG", parar)
    End Sub

    Sub CapNhap(ByVal PG As PhieuGiaoDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@MaSP", PG.MaHang)
        para(1) = New SqlParameter("@ngayxuat", PG.NgayGiao)
        para(2) = New SqlParameter("@makho", PG.MaKho)
        para(3) = New SqlParameter("@soluong", PG.SoLuong)
        cn.ExecuteNonQuery("CapNhapPG", para)
    End Sub

End Class
